package cn.mazexal.exambg.controller;

import cn.mazexal.exambg.common.models.*;
import cn.mazexal.exambg.models.bo.CustomerError;
import cn.mazexal.exambg.models.db.SysChannelDO;
import cn.mazexal.exambg.models.db.VideoMenuIntroDO;
import cn.mazexal.exambg.models.qry.*;
import cn.mazexal.exambg.models.vo.*;
import cn.mazexal.exambg.services.CommonService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 配置相关
 *
 * @author: Mazexal
 * Date: 2025/5/14
 */
@RestController
@RequestMapping("/common")
public class ConfigController {

    @Resource
    private CommonService commonService;

    @PostMapping("/getCardMenu")
    @Tag(name = "getCardMenu", description = "获取卡组菜单")
    public MultiResponse<CardMenuVO> getCardMenuVOList(@RequestBody IDQry idQry) {
        List<CardMenuVO> cardMenuVOList = commonService.getCardMenuVOList(idQry.getId());
        return MultiResponse.of(cardMenuVOList);
    }

    @PostMapping("/addCardMenu")
    @Tag(name = "addCardMenu", description = "添加卡组菜单")
    public SingleResponse<Boolean> addCardMenu(@RequestBody CardMenuVO cardMenuVO) {
        boolean result = commonService.addCardMenu(cardMenuVO);
        return SingleResponse.of(result);
    }

    @PostMapping("/updateCardMenu")
    @Tag(name = "updateCardMenu", description = "修改卡组菜单名称")
    public SingleResponse<Boolean> updateCardMenuName(@RequestBody CardMenuVO cardMenuVO) {
        boolean result = commonService.updateCardMenu(cardMenuVO);
        return SingleResponse.of(result);
    }

    @PostMapping("/deleteCardMenu")
    @Tag(name = "deleteCardMenu", description = "删除卡组菜单")
    public SingleResponse<Boolean> deleteCardMenu(@RequestBody IDQry idQry) {
        boolean result = commonService.deleteCardMenu(idQry.getId());
        return SingleResponse.of(result);
    }


    @PostMapping("/getTagList")
    @Tag(name = "getTagList", description = "获取标签列表")
    public PagedResponse<TagVO> getTagList(@RequestBody TagQry qry) {
        PagedData<TagVO> res = commonService.getTagVOList(qry);
        return PagedResponse.of(res);
    }

    @PostMapping("/addTag")
    @Tag(name = "addTag", description = "添加标签")
    public SingleResponse<Boolean> addTag(@RequestBody TagVO tagVO) {
        boolean result = commonService.addTag(tagVO);
        return SingleResponse.of(result);
    }

    @PostMapping("/updateTag")
    @Tag(name = "updateTag", description = "修改标签")
    public SingleResponse<Boolean> updateTag(@RequestBody TagVO tagVO) {
        boolean result = commonService.updateTag(tagVO);
        return SingleResponse.of(result);
    }

    @PostMapping("/deleteTag")
    @Tag(name = "deleteTag", description = "删除标签")
    public SingleResponse<Boolean> deleteTag(@RequestBody IDQry idQry) {
        boolean result = commonService.deleteTag(idQry.getId());
        return SingleResponse.of(result);
    }


    @PostMapping("/getFeedbackList")
    @Tag(name = "getFeedbackList", description = "获取反馈列表")
    public PagedResponse<UserFeedbackVO> getFeedbackList(@RequestBody FeedbackQry qry) {
        PagedData<UserFeedbackVO> res = commonService.getFeedbackList(qry);
        return PagedResponse.of(res);
    }

    @PostMapping("/handleFeedback")
    @Tag(name = "handleFeedback", description = "处理反馈")
    public SingleResponse<Boolean> handleFeedback(@RequestBody IDQry qry) {
        boolean res = commonService.handleFeedback(qry.getId());
        return SingleResponse.of(res);
    }


    @PostMapping("/getVideoMenuList")
    @Tag(name = "getVideoMenuList", description = "获取视频菜单列表")
    public MultiResponse<VideoMenuVO> getVideoMenuList(@RequestBody IDQry qry) {
        List<VideoMenuVO> res = commonService.getVideoMenuList(qry);
        return MultiResponse.of(res);
    }

    @PostMapping("/addVideoMenu")
    @Tag(name = "addVideoMenu", description = "添加视频菜单")
    public SingleResponse<Boolean> addVideoMenu(@RequestBody VideoMenuVO videoMenuVO) {
        if (videoMenuVO.getMenuType() == 1 && StringUtils.isNotBlank(videoMenuVO.getVideoUrl())) {
            throw new BizException(new CustomerError("500", "文件夹类型不能添加视频地址"));
        }
        boolean res = commonService.addVideoMenu(videoMenuVO);
        return SingleResponse.of(res);
    }

    @PostMapping("/updateVideoMenu")
    @Tag(name = "updateVideoMenu", description = "修改视频菜单")
    public SingleResponse<Boolean> updateVideoMenu(@RequestBody VideoMenuVO videoMenuVO) {
        if (videoMenuVO.getMenuType() == 1 && StringUtils.isNotBlank(videoMenuVO.getVideoUrl())) {
            throw new BizException(new CustomerError("500", "文件夹类型不能添加视频地址"));
        }
        boolean res = commonService.updateVideoMenu(videoMenuVO);
        return SingleResponse.of(res);
    }

    @PostMapping("/deleteVideoMenu")
    @Tag(name = "deleteVideoMenu", description = "删除视频菜单")
    public SingleResponse<Boolean> deleteVideoMenu(@RequestBody IDQry idQry) {
        boolean res = commonService.deleteVideoMenu(idQry.getId());
        return SingleResponse.of(res);
    }

    @PostMapping("/getVideoMenuIntroList")
    @Tag(name = "getVideoMenuIntroList", description = "获取视频菜单简介列表")
    public PagedResponse<VideoMenuIntroVO> getVideoMenuIntroList(@RequestBody VideoMenuIntroQry qry) {
        PagedData<VideoMenuIntroVO> res = commonService.getVideoMenuIntroList(qry);
        return PagedResponse.of(res);
    }

    @PostMapping("/addVideoMenuIntro")
    @Tag(name = "addVideoMenuIntro", description = "添加视频菜单简介")
    public SingleResponse<Boolean> addVideoMenuIntro(@RequestBody VideoMenuIntroVO videoMenuIntroVO) {
        boolean res = commonService.addVideoMenuIntro(videoMenuIntroVO);
        return SingleResponse.of(res);
    }

    @PostMapping("/updateVideoMenuIntro")
    @Tag(name = "updateVideoMenuIntro", description = "修改视频菜单简介")
    public SingleResponse<Boolean> updateVideoMenuIntro(@RequestBody VideoMenuIntroVO videoMenuIntroVO) {
        boolean res = commonService.updateVideoMenuIntro(videoMenuIntroVO);
        return SingleResponse.of(res);
    }

    @PostMapping("/deleteVideoMenuIntro")
    @Tag(name = "deleteVideoMenuIntro", description = "删除视频菜单简介")
    public SingleResponse<Boolean> deleteVideoMenuIntro(@RequestBody IDQry idQry) {
        boolean res = commonService.deleteVideoMenuIntro(idQry.getId());
        return SingleResponse.of(res);
    }

    @PostMapping("/getSysPayConfigList")
    @Tag(name = "getSysPayConfigList", description = "获取支付配置列表")
    public MultiResponse<SysPayConfigVO> getSysPayConfigList() {
        List<SysPayConfigVO> res = commonService.getSysPayConfigList();
        return MultiResponse.of(res);
    }

    @PostMapping("/addSysPayConfig")
    @Tag(name = "addSysPayConfig", description = "添加支付配置")
    public SingleResponse<Boolean> addSysPayConfig(@RequestBody SysPayConfigVO sysPayConfigVO) {
        boolean res = commonService.addSysPayConfig(sysPayConfigVO);
        return SingleResponse.of(res);
    }

    @PostMapping("/updateSysPayConfig")
    @Tag(name = "updateSysPayConfig", description = "修改支付配置")
    public SingleResponse<Boolean> updateSysPayConfig(@RequestBody SysPayConfigVO sysPayConfigVO) {
        boolean res = commonService.updateSysPayConfig(sysPayConfigVO);
        return SingleResponse.of(res);
    }


    @PostMapping("/getChannelList")
    @Tag(name = "getChannelList", description = "获取渠道列表")
    public MultiResponse<SysChannelVO> getChannelList() {
        return MultiResponse.of(commonService.getChannelList());
    }

    @PostMapping("/addChannel")
    @Tag(name = "addChannel", description = "添加渠道")
    public SingleResponse<Boolean> addChannel(@Validated @RequestBody SysChannelVO sysChannelVO) {
        boolean res = commonService.addChannel(sysChannelVO);
        return SingleResponse.of(res);
    }

    @PostMapping("/updateChannel")
    @Tag(name = "updateChannel", description = "更新渠道")
    public SingleResponse<Boolean> updateChannel(@Validated @RequestBody SysChannelVO sysChannelVO) {
        boolean res = commonService.updateChannel(sysChannelVO);
        return SingleResponse.of(res);
    }

    @PostMapping("/getChannelVersionList")
    @Tag(name = "getChannelVersionList", description = "获取渠道版本列表")
    public PagedResponse<SysChannelVersionVO> getChannelVersionList(@RequestBody ChannelVersionQry qry) {
        return PagedResponse.of(commonService.getChannelVersionList(qry));
    }

    @PostMapping("/addChannelVersion")
    @Tag(name = "addChannelVersion", description = "添加渠道版本")
    public SingleResponse<Boolean> addChannelVersion(@RequestBody SysChannelVersionVO sysChannelVersionVO) {
        sysChannelVersionVO.setVersion(handlerVersionStr(sysChannelVersionVO.getVersionStr()));
        return SingleResponse.of(commonService.addChannelVersion(sysChannelVersionVO));
    }

    private Integer handlerVersionStr(String versionStr){
        if(StringUtils.isBlank(versionStr)){
            return 0;
        }
        return Integer.parseInt(versionStr.replaceAll("\\.", ""));
    }

    @PostMapping("/updateChannelVersion")
    @Tag(name = "updateChannelVersion", description = "更新渠道版本")
    public SingleResponse<Boolean> updateChannelVersion(@RequestBody SysChannelVersionVO sysChannelVersionVO) {
        sysChannelVersionVO.setVersion(handlerVersionStr(sysChannelVersionVO.getVersionStr()));
        return SingleResponse.of(commonService.updateChannelVersion(sysChannelVersionVO));
    }

    @Tag(name = "getSysBroadcastList", description = "获取系统广播列表")
    @PostMapping("/getSysBroadcastList")
    public PagedResponse<SysBroadcastVO> getSysBroadcastList(@RequestBody BroadcastQry qry) {
        PagedData<SysBroadcastVO> pagedData = commonService.getSysBroadcastList(qry);
        return PagedResponse.of(pagedData);
    }


    @Tag(name = "updateSysBroadcast", description = "更新系统广播")
    @PostMapping("/updateSysBroadcast")
    public SingleResponse updateSysBroadcast(@RequestBody SysBroadcastVO sysBroadcastVO) {
        commonService.updateSysBroadcast(sysBroadcastVO);
        return SingleResponse.success();
    }
    @Tag(name = "addSysBroadcast", description = "添加系统广播")
    @PostMapping("/addSysBroadcast")
    public SingleResponse addSysBroadcast(@RequestBody SysBroadcastVO sysBroadcastVO) {
        commonService.addSysBroadcast(sysBroadcastVO);
        return SingleResponse.success();
    }

}
